package com.qipay.utils;

import org.springframework.stereotype.Component;

/**
 * Creator: Yao
 * Date:    2017/6/12
 * For:
 * Other:
 */
@Component
public class MathUtils {

    public static double round2(double val) {
        return Math.round(val * 100) / 100.0;
    }
    public static double getRandom(int min, int max) {
        return Math.round(Math.random() * (max - min) + min);
    }

    public static String changeBranch(String amount) {
        String currency = amount.replaceAll("\\$|\\￥|\\,", ""); // 处理包含, ￥
        // 或者$的金额
        int index = currency.indexOf(".");
        int length = currency.length();
        Long amLong;
        if (index == -1) {
            amLong = Long.valueOf(currency + "00");
        } else if (length - index >= 3) {
            amLong = Long.valueOf((currency.substring(0, index + 3)).replace(".", ""));
        } else if (length - index == 2) {
            amLong = Long.valueOf((currency.substring(0, index + 2)).replace(".", "") + 0);
        } else {
            amLong = Long.valueOf((currency.substring(0, index + 1)).replace(".", "") + "00");
        }
        return amLong.toString();
    }
    public static void main(String[] args) {
        double val = 1.2300001d;
        System.out.println(MathUtils.round2(val));
        val = 1234.4321d;
        System.out.println(MathUtils.round2(val));
        val = 1234.1d;
        System.out.println(MathUtils.round2(val));
        val = 1.105d;
        System.out.println(MathUtils.round2(val));
    }
}
